import React, {Component} from 'react';
import {StyleSheet, ScrollView, Text, View, TouchableOpacity, Alert, FlatList, RefreshControl, ActivityIndicator} from 'react-native';
import {List, Tabs, WingBlank, Flex, Toast, Modal, PickerView, Button, InputItem} from '@ant-design/react-native';
import {getBuildingListForAgent, setCompanyAgentVerify, setCompanyBuildVerify} from '~/api/company';
import {connect} from 'react-redux';
const mapGetter = (state) => {
    return {
        userinfo: state.userinfo,
        system: state.system,
    };
};
const renderContent = (tab, index) => {
    const style = {
        paddingVertical: 40,
        justifyContent: 'center',
        alignItems: 'center',
        margin: 10,
        backgroundColor: '#ddd',
    };
};

let pagecount = 0;
let index = 1;
let size = 10;
class CompanyBuilding extends React.Component {
    static navigationOptions = {
        title: '我的楼盘',
    };

    constructor(porps) {
        //调用基类的所有的初始化方法
        super(porps);
        // 设置当前组件的属性
        this.state = {
            list: [],
            tabvalue: 0,
            index: 0,
            page_index: 1,
            page_size: 10,
            isLoading: false, //加载
            refreshing: false,
            footer: 0, //上拉底部显示
            value: undefined,
            visible: false,
            buildlist: [],
            buildId: 0,
            buildName: '',
            totalcount: 0,
            tabs: [
                {title: '全部楼盘', value: 0},
                {title: '待审核', value: 6},
                {title: '销售中', value: 1},
                {title: '审核失败', value: 2},
            ],
            tabkey: 0,
            textinfo: '',
            companybuildid: '',
        };
        this.TabClick = this.TabClick.bind(this);
        this.onClose = () => {
            this.setState({
                visible: false,
            });
        };
    }
    TabClick(tab) {
        this.setState(
            {
                tabkey: tab.value === 0 ? 'all' : tab.value,
            },
            () => {
                this.GetList();
            },
        );
    }
    setTextinfo = (text) => {
        this.setState({
            textinfo: text,
        });
    };
    comfire(id) {
        this.setState({
            visible: true,
            companybuildid: id,
        });
    }
    comfirebuilding(state) {
        setCompanyBuildVerify({
            id: this.state.companybuildid, //楼盘id
            state: state,
            note: this.state.textinfo,
        }).then((r) => {
            this.onClose();
            if (r.code === 0) {
                this.GetList();
            } else {
                Toast.info(r.msg);
            }
        });
    }

    GetList() {
        getBuildingListForAgent({
            page_index: this.state.page_index,
            page_size: this.state.page_size,
            condidate: {
                tabkey: this.state.tabkey,
            },
        }).then((r) => {
            if (r.code === 0) {
                this.setState({
                    list: r.data.list,
                    totalcount: r.data.total_item_count,
                    footer: 0,
                });
                if (this.state.page_index === 1) {
                    pagecount = r.data.total_page_count;
                    this.setState({
                        footer: r.data.total_item_count < 10 ? 2 : 0,
                    });
                }
            }
        });
    }
    _onRefresh() {
        this.setState({footer: 0, page_index: index, page_size: size, buildId: 0, buildName: ''}, () => {
            this.GetList();
        });
    }
    //上拉加载更多
    loadData = () => {
        this.setState({isLoading: false});
        let dataArray = [];
        if (pagecount - 1 > 0) {
            pagecount = pagecount - 1;
            this.setState(
                {
                    page_index: this.state.page_index + 1,
                    footer: 1,
                },
                () => {
                    getBuildingListForAgent({
                        page_index: this.state.page_index,
                        page_size: this.state.page_size,
                        condidate: {
                            tabkey: this.state.tabkey,
                        },
                    }).then((r) => {
                        dataArray = r.data.list;
                        dataArray = this.state.list.concat(dataArray);
                        if (r.code === 0) {
                            this.setState({
                                list: dataArray,
                                isLoading: false,
                            });
                        }
                    });
                },
            );
        } else {
            this.setState({
                footer: 2,
            });
        }
    };

    //上拉底部显示
    genIndicator() {
        if (this.state.footer === 1) {
            return (
                <View style={styles.indicatorContainer}>
                    <ActivityIndicator style={styles.indicator} size={'large'} animating={true} />
                    <Text style={{textAlign: 'center'}}>正在加载更多</Text>
                </View>
            );
        } else if (this.state.footer === 2) {
            return (
                <View
                    style={{
                        height: 30,
                        alignItems: 'center',
                        justifyContent: 'flex-start',
                    }}>
                    <Text
                        style={{
                            color: '#999999',
                            fontSize: 14,
                            marginTop: 5,
                            marginBottom: 5,
                        }}>
                        没有更多数据了
                    </Text>
                </View>
            );
        } else if (this.state.footer === 0) {
            return (
                <View
                    style={{
                        height: 30,
                        alignItems: 'center',
                        justifyContent: 'flex-start',
                    }}>
                    <Text
                        style={{
                            color: '#999999',
                            fontSize: 14,
                            marginTop: 5,
                            marginBottom: 5,
                        }}>
                        上拉加载更多
                    </Text>
                </View>
            );
        }
    }
    _renderItem(item) {
        return (
            <WingBlank
                size="md"
                style={{
                    backgroundColor: 'white',
                    marginTop: 10,
                    borderRadius: 10,
                }}>
                <Flex
                    onPress={() => {
                        item.BuildingState != 6 && this.props.navigation.push('MyOwner', {buildId: item.CompanyBuildingID});
                    }}
                    direction={'column'}
                    style={{
                        width: '100%',
                        paddingTop: '3%',
                        paddingBottom: 10,
                    }}>
                    <Flex
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}
                        justify={'between'}>
                        <Text numberOfLines={1} style={{color: '#666666', fontWeight: 'bold'}}>
                            {item.BuildingName}
                        </Text>
                        <Text numberOfLines={1} style={{color: '#ec4f46', fontWeight: 'bold'}}>
                            {item.BuildingState === 1 && '销售中'}
                            {item.BuildingState === 2 && '失效'}
                            {item.BuildingState === 3 && '草稿'}
                            {item.BuildingState === 4 && '取消'}
                            {item.BuildingState === 5 && '作废'}
                            {item.BuildingState === 6 && item.AgentState === 6 && '待审'}
                            {item.BuildingState === 6 && item.AgentState === 1 && '初审通过 平台待审'}
                            {item.BuildingState === 6 && item.AgentState === 7 && '初审失败'}
                            {item.BuildingState === 7 && '审核失败'}
                        </Text>
                    </Flex>
                    <View style={{width: '100%', height: 1, backgroundColor: '#EEEEEE', marginTop: 10}} />
                    <Flex
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}
                        justify={'between'}>
                        <Text
                            style={{
                                fontSize: 15,
                                color: '#666666',
                            }}>
                            公司名称：
                        </Text>
                        <Text numberOfLines={1} style={{color: '#333333', fontWeight: 'bold'}}>
                            {item.CompanyName}
                        </Text>
                    </Flex>
                    <Flex
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}
                        justify={'between'}>
                        <Text style={{fontSize: 15, color: '#666666'}}>联系人：</Text>
                        <Text
                            style={{
                                color: '#333333',
                                fontWeight: 'bold',
                            }}>
                            {item.Contacter}
                        </Text>
                    </Flex>
                    <Flex
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}
                        justify={'between'}>
                        <Text
                            style={{
                                fontSize: 15,
                                color: '#666666',
                            }}>
                            联系电话：
                        </Text>
                        <Text style={{color: '#333333', fontWeight: 'bold'}}>{item.Telephone}</Text>
                    </Flex>
                    <Flex
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                            marginBottom: 10,
                        }}
                        justify={'between'}>
                        <Text style={{fontSize: 15, color: '#666666'}}>地址：</Text>
                        <Text
                            numberOfLines={1}
                            style={{
                                color: '#333333',
                                fontWeight: 'bold',
                                flexWrap: 'wrap',
                            }}>
                            {item.CityName}
                            {item.Address}
                        </Text>
                    </Flex>
                    <View style={{width: '100%', height: 1, backgroundColor: '#EEEEEE', marginTop: 10}} />
                    <Flex>
                        <Flex
                            style={{
                                paddingLeft: 15,
                                paddingRight: 15,
                                paddingTop: 9,
                            }}
                            justify={'end'}>
                            <Text
                                onPress={() => {
                                    this.props.navigation.push('CommunityDetails', {
                                        params: item.ProjectDataID,
                                        buy: item.BuyKind === 1 ? item.CompanyBuildingID : 0,
                                        companyid: item.CompanyBuildingID,
                                    });
                                }}
                                style={styles.textbuttom5}>
                                我的楼盘
                            </Text>
                        </Flex>
                        {item.BuildingState != 6 && (
                            <Flex
                                onPress={() => {
                                    this.props.navigation.push('MyOwner', {buildId: item.CompanyBuildingID});
                                }}
                                style={{
                                    paddingLeft: 15,
                                    paddingRight: 15,
                                    paddingTop: 9,
                                }}
                                justify={'end'}>
                                <Text style={styles.textbuttom5}>我的业主</Text>
                            </Flex>
                        )}
                        {item.AgentState === 6 && item.BuildingState === 6 && this.props.userinfo.identity === 31 && (
                            <>
                                <Flex
                                    style={{
                                        paddingLeft: 15,
                                        paddingRight: 15,
                                        paddingTop: 9,
                                    }}
                                    justify={'end'}>
                                    <Text
                                        onPress={() => {
                                            item.AgentState === 6 && this.comfire(item.CompanyBuildingID);
                                        }}
                                        style={styles.textbuttom5}>
                                        {item.AgentState === 6 && '待审'}
                                    </Text>
                                </Flex>
                                <Modal popup visible={this.state.visible} animationType="slide-up" onClose={this.onClose}>
                                    <Text style={{margin: 20}}>确认通过该楼盘的审核？</Text>
                                    <Text style={{padding: 20}}>审核意见</Text>
                                    <InputItem placeholder="请输入审核意见" placeholderTextColor="gray" type="text" onChange={this.setTextinfo} />
                                    <Flex justify={'around'}>
                                        <Button
                                            onPress={() => {
                                                this.comfirebuilding(7);
                                            }}
                                            style={{
                                                width: '45%',
                                                marginTop: 30,
                                                backgroundColor: '#F2BC38',
                                                borderColor: '#F2BC38',
                                            }}
                                            type={'primary'}>
                                            审核不通过
                                        </Button>
                                        <Button
                                            style={{width: '45%', marginTop: 30}}
                                            type={'warning'}
                                            onPress={() => {
                                                this.comfirebuilding(1);
                                            }}>
                                            审核通过
                                        </Button>
                                    </Flex>
                                </Modal>
                            </>
                        )}
                    </Flex>
                </Flex>
            </WingBlank>
        );
    }

    render() {
        return (
            <View style={{flex: 1}}>
                <Tabs tabs={this.state.tabs} prerenderingSiblingsNumber={0} initialPage={this.state.index} onChange={this.TabClick}>
                    <FlatList
                        data={this.state.list}
                        style={{width: '100%'}}
                        showsVerticalScrollIndicator={false}
                        renderItem={(data) => this._renderItem(data.item)}
                        keyExtractor={(item, index) => index}
                        //ListHeaderComponent={this._HeaderView}
                        ListFooterComponent={() => this.genIndicator()} //上拉加载更多视图
                        onEndReached={() => {
                            this.loadData();
                        }}
                        onEndReachedThreshold={0.1} // 这个属性的意思是 当滑动到距离列表内容底部不足 0.1*列表内容高度时触发onEndReached函数 为啥要加这个属性 因为不加的话滑动一点点就会立即触发onReached函数，看不到菊花加载中
                        refreshing={true}
                        refreshControl={
                            <RefreshControl
                                refreshing={this.state.refreshing}
                                onRefresh={() => {
                                    this._onRefresh();
                                }}
                            />
                        }
                    />
                </Tabs>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    textbuttom5: {
        fontSize: 14,
        width: 80,
        height: 30,
        borderWidth: 1,
        borderColor: '#Ec4f46',
        borderRadius: 15,
        color: '#Ec4f46',
        textAlign: 'center',
        paddingTop: 5,
        marginLeft: 5,
    },
});

export default connect(mapGetter)(CompanyBuilding);
